import { troggleActive } from "../../common.js"

class ButtonGroup {
    constructor() {
        this.beforeSelectedEvent = () => {}
        this.selectedEvent = () => {}
    }
    init() {
        const groups = document.querySelectorAll('.gd-btn-group')
        groups.forEach(group => {
            const btns = group.querySelectorAll('.gd-btn')
            btns.forEach((btn, i) => {
                btn.addEventListener('click', event => {
                    if(this.beforeSelectedEvent(event, i) !== false) {
                        troggleActive(btn, () => {
                            this.selectedEvent(event, i)
                        })
                    }
                })
            })
        })
    }
    setBeforeSelectedEvent(fn) {
        this.beforeSelectedEvent = fn
    }
    setSelectedEvent(fn) {
        this.selectedEvent = fn
    }
}
export default ButtonGroup